#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+7;
typedef long long ll;
ll dp[MAXN],a[MAXN],T,n,vis[MAXN],pre[MAXN],viss[MAXN];
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin >> T;
    while(T--){
        memset(dp,0,sizeof(dp));
        memset(vis,0,sizeof(vis));
        memset(viss,0,sizeof(viss));
        cin >> n;
        for (int i=1;i<=n;i++){
            cin >> a[i];
            if (vis[a[i]]){pre[i]=vis[a[i]];}
            if (!vis[a[i]]){vis[a[i]]=i;}
        }
        for (int i=1;i<=n;i++){
            if (vis[a[i]]>=i){dp[i]=dp[i-1];continue;}
            if (dp[vis[a[i]]]+a[i]>dp[i-1]){
                dp[i]=max(dp[vis[a[i]]]+a[i],dp[i]);
//                tmp2=i;tmp1=vis[a[i]];
                pre[i]=vis[a[i]];
                vis[a[i]]=i;
//          cout << i << " " << dp[i] <<" "<< a[i] << endl;
            }
            for (int j=i-1;j>=1;j--){
                if (pre[i]<pre[j]&&pre[j]==j-1){
                    dp[i]=max(dp[j]+a[i],dp[i]);
                }
                else if (pre[i]>pre[j]&&pre[j]!=0){
                    if (pre[i]==j-1){
                        dp[i]=max(dp[j]+a[i],dp[i]);
//                        cout << i << " " << dp[i]<< " " << j<<" "<<dp[j]<<" " << dp[j]+a[i] <<endl;
                    }
                    else{
                        dp[i]=max(max(dp[pre[i]]+a[i],dp[j]),dp[i]);
                    }
                }
                dp[i]=max(dp[i-1],dp[i]);
            }
        }
        cout << dp[n] << endl;
    }
    return 0;
}